package dto.api;

import java.util.*;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import models.application.*;
import models.pubobj.*;
import services.AniPublicPropService;
import services.exception.*;

/**
 * User: yeh
 * Date: 2/17/13
 * Time: 7:13 PM
 * For:  wilddragon
 */
public class ApiParamDto {
    public Long id;

    public Long hostApiId;

    public String paramName;

    public String paramValue;

    public Boolean isRequired = false;

    public String description;

    public static String[] requiredRegisterFieldKeys = {
            //"hostApiId",
            "paramName",
            //"isRequired"
    };
    public static String[] requiredUpdateFieldKeys = {
            "id"
    };

    public ApiParamDto(Long id, Long hostApiId, String paramName, String paramValue, Boolean required, String description) {
        this.id = id;
        this.hostApiId = hostApiId;
        this.paramName = paramName;
        this.paramValue = paramValue;
        isRequired = required;
        this.description = description;
    }

    public ApiParamDto(Long hostApiId, String paramName, String paramValue, Boolean required, String description) {
        this.hostApiId = hostApiId;
        this.paramName = paramName;
        this.paramValue = paramValue;
        isRequired = required;
        this.description = description;
    }

    public static ApiParamDto generateFromDao(AniApiParam oneParam){
        return new ApiParamDto(
                oneParam.id,
                oneParam.hostApi.id,
                oneParam.paramName,
                oneParam.paramValue,
                oneParam.isRequired,
                oneParam.description
        );
    }

    public static List<ApiParamDto> generateFromDaoList(List<AniApiParam> paramsDao){
        List<ApiParamDto> paramsDto = new ArrayList<ApiParamDto>();
        for(AniApiParam oneParamDao: paramsDao){
            paramsDto.add(generateFromDao(oneParamDao));
        }
        return paramsDto;
    }

    public static List<ApiParamDto> generateFromJsonArrayStr(String apiParamJsonArray, AniApi oneApi){
        Gson jsonGenerator = new Gson();
        List<ApiParamDto> apiParams = null;
        try{
            apiParams = jsonGenerator.fromJson(apiParamJsonArray, new TypeToken<ArrayList<ApiParamDto>>(){}.getType());
            if(apiParams != null && !apiParams.isEmpty()){
                for(ApiParamDto oneParam: apiParams){
                    //oneParam.id = oneApi.id;
                    oneParam.isRequired = oneParam.isRequired == null ? false : true;
                }
            }

        }catch (Exception e){
            throw new AniDataException("API_PARAM_JSON_FORMAT_ERROR");
        }
        return apiParams;
    }

    public AniApiParam getApiParamDao(AniApi hostApi){
        return new AniApiParam(
                this.id,
                hostApi,
                this.paramName,
                this.paramValue,
                this.isRequired,
                this.description
        );
    }

    public AniApi generateHostApiDao(){
        return new AniApi(this.hostApiId);
    }

    public static List<AniApiParam> getApiParamsDao(List<ApiParamDto> paramDtos, AniApi hostApi){
        List<AniApiParam> apiParamsDao = new ArrayList<AniApiParam>();
        for(ApiParamDto oneParamDto: paramDtos){
            apiParamsDao.add(oneParamDto.getApiParamDao(hostApi));
        }
        return apiParamsDao;
    }

    public static List<String> getParamsName(List<ApiParamDto> params){
        List<String> paramNames = new ArrayList<String>();
        for(ApiParamDto oneParam: params){
            paramNames.add(oneParam.paramName);
        }
        return paramNames;
    }
}
